﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using Microsoft.ServiceBus;

namespace nwrole.lib2 {
    public class Orchestrator {
        private ServiceHost serviceHost;
        private OrchestratorService orchestrator;

        public void Start() {
            ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;

            //Console.WriteLine("Starting place {0}...", place.Folder.Name);

            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;
            sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = "owner";
            sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = "ihXdEKIJtg8jByN1aITLDX99eiEgMeHOfr4ppzX5MfM=";

            Uri clientServiceBaseAddress = ServiceBusEnvironment.CreateServiceUri("sb", "systemevolutiontestfive", "nwrole/Orchestrator");

            orchestrator = new OrchestratorService();
            serviceHost = new ServiceHost(orchestrator, clientServiceBaseAddress);
            try {
                IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);

                ContractDescription clientContractDescription = ContractDescription.GetContract(typeof(IOrchestratorContract), typeof(OrchestratorService));
                ServiceEndpoint clientServiceEndPoint = new ServiceEndpoint(clientContractDescription);
                clientServiceEndPoint.Address = new EndpointAddress(clientServiceBaseAddress);
                clientServiceEndPoint.Binding = new NetTcpRelayBinding();
                clientServiceEndPoint.Binding = new NetTcpRelayBinding(EndToEndSecurityMode.Transport, RelayClientAuthenticationType.RelayAccessToken);
                serviceHost.Description.Endpoints.Add(clientServiceEndPoint);

                /*ContractDescription ctrlClientContractDescription = ContractDescription.GetContract(typeof(IOrchestratorCtrlContract), typeof(OrchestratorCtrlService));
                ServiceEndpoint ctrlClientServiceEndPoint = new ServiceEndpoint(ctrlClientContractDescription);
                ctrlClientServiceEndPoint.Address = new EndpointAddress(baseAddress);
                ctrlClientServiceEndPoint.Binding = new NetTcpRelayBinding();
                ctrlClientServiceEndPoint.Binding = new NetTcpRelayBinding(EndToEndSecurityMode.Transport, RelayClientAuthenticationType.RelayAccessToken);
                serviceHost.Description.Endpoints.Add(ctrlClientServiceEndPoint);*/

                foreach (ServiceEndpoint endpoint in serviceHost.Description.Endpoints) {
                    endpoint.Behaviors.Add(serviceRegistrySettings);
                    endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
                }

                serviceHost.Open();

                //Console.WriteLine("Service address: {0}", baseAddress);
            } catch (CommunicationException ce) {
                //Console.WriteLine("An exception occurred with place {0}: {1}", place.Folder.Name, ce.Message);
                serviceHost.Abort();
            }
        }

        public void Stop() {
            if (serviceHost != null) {
                //orchestrator.Stop();
                serviceHost.Close();
                serviceHost = null;
                orchestrator = null;
            }
        }
    }
}
